package com.rest.demo.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author Administrator
 * @decription
 * @Date 2018-02-08 14:04
 */
public class SocketChannelDemo {
    public static void main(String[] args) throws IOException {
        SocketChannel channel = SocketChannel.open();
        channel.connect(new InetSocketAddress("http://jenkov.com", 80));
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        int b = -1;
        while(! channel.finishConnect()) {
            buffer.flip();
            byte[] bytes = new byte[1024];
            int i = 0;
            while (buffer.hasRemaining()) {
                bytes[i++] = buffer.get();
            }
            System.out.println(new String(bytes,0,b));
            buffer.clear();
        }
        channel.close();
    }
}
